home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / unlink.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  3KB  |  97 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  unlink.c,v 1.1.1.1 1994/04/04 04:30:37 amiga Exp
  20.  *
  21.  *  unlink.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:37  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  26.  *  Initial revision
  27.  *
  28.  */
  29.  
  30. #define KERNEL
  31. #include "ixemul.h"
  32. #include "kprintf.h"
  33. #include <string.h>
  34.  
  35. static int
  36. __delete_func (struct StandardPacket *sp, struct MsgPort *handler,
  37.                BPTR parent_lock,
  38.            BSTR name,
  39.            void *dummy, int *no_error)
  40. {
  41.   sp->sp_Pkt.dp_Type = ACTION_DELETE_OBJECT;
  42.   sp->sp_Pkt.dp_Arg1 = parent_lock;
  43.   sp->sp_Pkt.dp_Arg2 = name;
  44.  
  45.   PutPacket (handler, sp);
  46.   __wait_sync_packet (sp);
  47.  
  48.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  49.  
  50.   /* stop if we failed because of symlink - reference */
  51.   return 0;
  52. }
  53.  
  54. int
  55. unlink (char *name)
  56. {
  57.   int err, i;
  58.   struct file **fp;
  59.   int result;
  60.   struct stat st;
  61.  
  62.   result = 0;
  63.  
  64.   if (syscall(SYS_stat, name, &st) == 0 && (st.st_mode & S_IWUSR) == 0)
  65.     syscall(SYS_chmod, name, st.st_mode | S_IWUSR);
  66.   /* first try to normally delete the file, if we get an
  67.    * 'object in use' error, we'll try another way out.. */
  68.   if (__plock (name, __delete_func, 0)) goto ret;
  69.  
  70.   /* so there was an error.. */
  71.   err = IoErr();
  72.   if (err == ERROR_OBJECT_IN_USE)
  73.     {
  74.       /* check whether the name is an absolute filename, if yes, we
  75.        * can walk thru our filetab to see, whether WE have this file
  76.        * open, and set its unlink-flag, if not, return the error */
  77.  
  78.       /* if we fully and consistent allow Unix-pathnames, we can test
  79.        * for a leading '/' too. */
  80.       if (index(name, ':') || ((ix.ix_flags & ix_translate_slash) && name[1] && name[0] == '/'))
  81.     {
  82.       for (fp = u.u_ofile, i=0; i < NOFILE; i++, fp++)
  83.         if (*fp && (*fp)->f_name && !strcmp((*fp)->f_name, name))
  84.           {
  85.         (*fp)->f_flags |= FUNLINK;
  86.         goto ret;
  87.           }
  88.     }
  89.     }
  90.   errno = __ioerr_to_errno(err);
  91.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  92.   result = -1;
  93.  
  94. ret:
  95.   return result;
  96. }
  97.